package hipt.com.dao.daoImpl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import hipt.com.dao.CategoryDao;
import hipt.com.model.Category;

@Repository("categoryDao")
public class CategoryDaoImpl implements CategoryDao {
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void deleteCategory(Category category) {
		sessionFactory.getCurrentSession().delete(category);
	}

	@Override
	public Category getCategoryById(Integer id) {
		return (Category) sessionFactory.getCurrentSession().get(Category.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> listCategories() {
		return sessionFactory.getCurrentSession().createQuery("from Category")
		.list();
	}

	@Override
	public void saveCategory(Category category) {
		sessionFactory.getCurrentSession().saveOrUpdate(category);
	}

	@Override
	public void updateCategory(Category category) {
		sessionFactory
		.getCurrentSession()
		.createQuery(
				"UPDATE Category SET name=:name ,description=:description where Id=:id")
		.setParameter("name", category.getName()).setParameter(
				"description", category.getDescription()).setParameter(
				"Id", category.getId())
		.executeUpdate();
	}
}
